﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using r4u.business.logic.layer.RECRUIT;
using r4u.data.access.layer;

namespace r4u.Controllers
{
    public class VacancyController : Controller
    {
        //
        // GET: /Vacancy/Create
        [HttpPost]
        public ActionResult Create(VACANCY  vacancy)
        {
            Vacancy temp = new Vacancy();

            if (temp.AddVacancy(HttpContext.User.Identity.Name, vacancy))
            {
                return new HttpStatusCodeResult(HttpStatusCode.OK);
            }
            else
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
            }
        }


        [HttpPost]
        public ActionResult Update(VACANCY vacancy)
        {
            Vacancy temp = new Vacancy();

            if (temp.UpdateVacancy(vacancy))
            {
                return new HttpStatusCodeResult(HttpStatusCode.OK);
            }
            else
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
            }
        }


        public ActionResult Get(int id)
        {
            Vacancy temp = new Vacancy();

            var settings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(temp.GetVacancy(id.ToString()), settings),
                ContentType = "application/json"
            };
            return jsonResult;
        }


        public ActionResult Copy(int id)
        {
            Vacancy temp = new Vacancy();

            var settings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(temp.Copy(HttpContext.User.Identity.Name, id.ToString()), settings),
                ContentType = "application/json"
            };
            return jsonResult;
        }


        public ActionResult Delete(int id)
        {
            Vacancy temp = new Vacancy();

            if (temp.DeleteVacancy(id.ToString()))
            {
                return new HttpStatusCodeResult(HttpStatusCode.OK);
            }
            else
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
            }
        }


        public ActionResult List()
        {
            Vacancy temp = new Vacancy();
            var settings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(temp.GetVacancies(HttpContext.User.Identity.Name), settings),
                ContentType = "application/json"
            };
            return jsonResult;
        }

    }
}
